﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class Sms_Template_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Users/Sms_Template_Edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public ISms_templateService _sms_template { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_sms_template", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            this.action = Request.Query["action"];

            if (!string.IsNullOrEmpty(this.action) && this.action == CMSEnums.ActionEnum.Edit.ToString())
            {
                int.TryParse(Request.Query["id"],out this.id);
                if (this.id == 0)
                {
                    ViewBag.ClientScript = JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_sms_template.Exists(this.id))
                {
                    ViewBag.ClientScript = JscriptMsg("记录不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }

        // GET: /admin/Sms_Template_Edit/

        public ActionResult Index()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_sms_template", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", string.Empty);
                }
                else
                {
                    ViewBag.ClientScript = JscriptMsg("修改短信模板成功！", "../sms_template_list/index");
                }
            }
            else //添加
            {
                ChkAdminLevel("user_sms_template", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", string.Empty);
                }
                else
                {
                    ViewBag.ClientScript = JscriptMsg("添加短信模板成功！", "../sms_template_list/index");
                }
            }
            return View(EDIT_RESULT_VIEW);
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var model = _sms_template.GetModel(_id);
            ViewData["model"] = model;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Sms_template model = new Sms_template();
            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.call_index = Request.Form["txtCallIndex"].FirstOrDefault().Trim();
            model.code = Request.Form["txtCode"];
            model.content = Request.Form["txtContent"];

            if (_sms_template.Add(model) > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加短信模板:" + model.title); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            var model = _sms_template.GetModel(_id);

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.call_index = Request.Form["txtCallIndex"].FirstOrDefault().Trim();
            model.content = Request.Form["txtContent"];
            model.code = Request.Form["txtCode"];

            if (_sms_template.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改短信模板:" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

    }
}
